package com.vgemv.jsutilitysdk.JsThread;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

public class JsThreadManager {

//    public static JsThreadManager sharedInstance = new JsThreadManager();


    //主线程的Handler
    private static final Handler mHandler_main = new Handler(Looper.getMainLooper());

    //主线程,通过主事件循环来获得主线程.
    private static final Thread mThread_main = Looper.getMainLooper().getThread();


    /**
     * 判断当前线程是否为主线程.
     * @return
     */
    public static boolean isCurrentThreadMain() {
        return mThread_main == Thread.currentThread();
    }

    /**
     * 如果当前就是主线程,立即执行,否则post到主线程执行.
     * @param runnableTask
     */
    public static void runInMainThreadImmediately(final Runnable runnableTask) {
        runInMainThread(0,true,runnableTask);
    }

    /**
     * 如果当前就是主线程,在下一个runloop执行.
     * @param runnableTask
     */
    public static void runInMainThreadAtNextRunloop(final Runnable runnableTask) {
        runInMainThread(0,false,runnableTask);
    }

    /**
     * 如果当前就是主线程,在下一个runloop的下一个runloop执行.
     * 因为有的系统处理,在下一个runloop的末尾,自己不一定能捕捉到.
     * @param runnableTask
     */
    public static void runInMainThreadAtNextNextRunloop(final Runnable runnableTask) {
        runInMainThread(0, false, new Runnable() {
            @Override
            public void run() {
                runInMainThread(0,false,runnableTask);
            }
        });
    }

    /**
     * 延迟在主线程执行.
     * @param delay 毫秒数
     */
    public static void runInMainThreadDelayed(long delay,final Runnable runnableTask) {
        runInMainThread(delay,false,runnableTask);
    }

    /**
     * 在主线程,延迟执行指定Runnable
     *
     * @param runnableTask 需要执行的任务
     * @param delay
     * @param isRunImmediately 如果当前就是主线程,是否立即执行.
     */
    public static final void runInMainThread( final long delay,boolean isRunImmediately,final Runnable runnableTask) {
        if (runnableTask == null){
            return;
        }
        if (delay<0){
            return;
        }
        //当前任务还没有执行,可以取消执行????
        mHandler_main.removeCallbacks(runnableTask);

        //当前线程不是主线程
        if(Thread.currentThread()!=mThread_main) {
            if(delay == 0) {
                mHandler_main.post(runnableTask);
            }
            else {
                mHandler_main.postDelayed(runnableTask,delay);
            }
        }
        //当前线程就是主线程
        else {
            if(delay == 0) {
                if(isRunImmediately) {
                    runnableTask.run();
                }
                else {
                    mHandler_main.post(runnableTask);
                }
            }
            else {
                mHandler_main.postDelayed(runnableTask,delay);
            }
        }
    }

    /**
     * 在子线程中执行
     * 如果不需要delay,立即创建一个新Thread执行任务.
     * 如果需要delay,则创建一个线程,开启looper,在指定时间后将runnable发过去.
     * @param delay
     * @param runnable
     */
    public static void runInSubThread(long delay,Runnable runnable) {
        if(delay == 0) {
            final Thread thread = new Thread(runnable);
            thread.start();
        }
        else {
            HandlerThread handlerThread = new HandlerThread("SubString");
            Looper looper = handlerThread.getLooper();
            Handler handler = new Handler(looper);
            handler.postDelayed(runnable,delay);
        }
    }

    //将taskA放到子线程,做完之后将任务放到主线程.
    public static void dispatchAsyncMainCallback(Runnable subThreadTask,Runnable mainThreadTask) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                subThreadTask.run();
                runInMainThreadImmediately(mainThreadTask);
            }
        }.start();
    }
}
